package com.example.bijidemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bijidemo.common.CustomException;
import com.example.bijidemo.common.R;
import com.example.bijidemo.dto.RenwuDto;
import com.example.bijidemo.entity.*;
import com.example.bijidemo.mapper.RenWuMapper;
import com.example.bijidemo.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class RenWuServiceImpl extends ServiceImpl<RenWuMapper, Renwu> implements RenWuService {
    @Autowired
    RenWuService renWuService;
    @Autowired
    RenwuWeiduService renwuWeiduService;
    @Autowired
    RenwuJinchengService renwuJinchengService;
    @Autowired
    EmployeeService employeeService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    RenWuMapper renWuMapper;


    @Override
    public List<Renwu> getReList() {
        List<Renwu> geta = renWuMapper.geta();
        return geta;
    }

    /**
     * 新增
     * @param renwuDto
     */
    @Transactional
    public void zengjia(RenwuDto renwuDto) throws ParseException {
        //拿到指派人的id
        Long zhiPaiId = renwuDto.getEmpId();
        Long fuzeRenId = renwuDto.getEmppId();
        Employee zhipairenone = employeeService.getById(zhiPaiId);
        Employee fuzerenone = employeeService.getById(fuzeRenId);
        //把负责人的id找到name
        String zpname = zhipairenone.getName();
        String fzname = fuzerenone.getName();
        //封装name到renwu表中



        //吧dto中的属行赋给renwu
        renWuService.save(renwuDto);
        //1.
//        Date jiezhishijian = renwuDto.getJiezhishijian();
//        System.out.println(jiezhishijian);
//
//
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
//
//        String dateTime = format.format(jiezhishijian);
//        dateTime=dateTime.replace("T"," ");
//        String substring = dateTime.substring(0, 19);
//        // dateTime = dateTime.replace("Z", " UTC");
//        SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//           // Date time = format.parse(dateTime);
//           // String result = defaultFormat.format(time);
//
//            Date parse = defaultFormat.parse(substring);
//            renwuDto.setJiezhishijian(parse);
//
//            renWuService.save(renwuDto);

        //拿到任务的id
        Long renwuid = renwuDto.getId();
        Renwu renwu = renWuService.getById(renwuid);
        renwu.setFuzeren(fzname);
        renwu.setZhipairen(zpname);
        renWuService.updateById(renwu);
        List<RenwuWeidu> flavors = renwuDto.getFlavors();
        flavors.stream().map((item) -> {
            item.setRenwuId(renwuid);
            return item;
        }).collect(Collectors.toList());

        //保存任务维度到任务维度表
        renwuWeiduService.saveBatch(flavors);
        //
        String jinchengValue = renwuDto.getValue();
        RenwuJincheng renwuJincheng=new RenwuJincheng();
        renwuJincheng.setValue(jinchengValue);
        renwuJincheng.setRenwuId(renwuid);
        //保存任务状态到任务状态表
       renwuJinchengService.save(renwuJincheng);
        log.info("dto中有{}",renwuDto);


    }

    @Override
    public RenwuDto getbyidwith(Long id) {
        //根据id查询任务
        RenwuDto renwuDto=new RenwuDto();
        Renwu renwu = renWuService.getById(id);
        BeanUtils.copyProperties(renwu,renwuDto);
        //根据任务id   查询标签信息
        LambdaQueryWrapper<RenwuWeidu> lqw=new LambdaQueryWrapper<>();
        lqw.eq(RenwuWeidu::getRenwuId,renwu.getId());
        List<RenwuWeidu> list = renwuWeiduService.list(lqw);
        renwuDto.setFlavors(list);
        //封装项目名称
        Long categoryId = renwuDto.getCategoryId();
        //根据id查询分类表中的分类名字
        Category category = categoryService.getById(categoryId);
        String name = category.getName();
        renwuDto.setCategoryName(name);
        //封装指派人负责人的员工id
        String zhipairen = renwu.getZhipairen();
        String fuzeren = renwu.getFuzeren();
        //根据name查询id
        LambdaQueryWrapper<Employee> a=new LambdaQueryWrapper<>();
        a.eq(Employee::getName,zhipairen);
        Employee one = employeeService.getOne(a);
        Long id1 = one.getId();
        renwuDto.setEmpId(id1);
        //设置负责人的id
        LambdaQueryWrapper<Employee> b=new LambdaQueryWrapper<>();
        b.eq(Employee::getName,fuzeren);
        Employee two=employeeService.getOne(b);
        Long id2 = two.getId();
        renwuDto.setEmppId(id2);
        //封装任务完成度
        LambdaQueryWrapper<RenwuJincheng> rj=new LambdaQueryWrapper<>();
        rj.eq(RenwuJincheng::getRenwuId,id);
        RenwuJincheng renwuJincheng = renwuJinchengService.getOne(rj);
        String value = renwuJincheng.getValue();
        renwuDto.setValue(value);


        return renwuDto;

    }

    @Override
    @Transactional
    public void updatebywithrenwu(RenwuDto renwuDto) {
        log.info(renwuDto.toString());
        //更新任务
        renWuService.updateById(renwuDto);
        //获取指派人负责人
        Long empId = renwuDto.getEmpId();
        Long emppId = renwuDto.getEmppId();
        //根据id查询员工表里面的name
        LambdaQueryWrapper<Employee> emp=new LambdaQueryWrapper<>();
        emp.eq(Employee::getId,empId);
        Employee zhipairen = employeeService.getOne(emp);
        String zhipairenName = zhipairen.getName();

        LambdaQueryWrapper<Employee> empp=new LambdaQueryWrapper<>();
        empp.eq(Employee::getId,emppId);
        Employee fuze=employeeService.getOne(empp);
        String fuzeName = fuze.getName();
        Long id = renwuDto.getId();
        Renwu renwu = renWuService.getById(id);
        renwu.setZhipairen(zhipairenName);
        renwu.setFuzeren(fuzeName);
        renWuService.updateById(renwu);
        //删除与任务关联的标签
        LambdaQueryWrapper<RenwuWeidu> a=new LambdaQueryWrapper<>();
        a.eq(RenwuWeidu::getRenwuId,renwuDto.getId());
        renwuWeiduService.remove(a);

        //插入与任务关联的标签
        List<RenwuWeidu> flavors = renwuDto.getFlavors();
           flavors= flavors.stream().map((item)->{
            item.setRenwuId(renwuDto.getId());
            return item;
        }).collect(Collectors.toList());


        renwuWeiduService.saveBatch(flavors);

        //操作完成度表进行完成度的修改、
        LambdaQueryWrapper<RenwuJincheng> jinchengLambdaQueryWrapper=new LambdaQueryWrapper<>();
        jinchengLambdaQueryWrapper.eq(RenwuJincheng::getRenwuId,id);
        RenwuJincheng renwuJincheng = renwuJinchengService.getOne(jinchengLambdaQueryWrapper);
        renwuJincheng.setValue(renwuDto.getValue());
        renwuJinchengService.updateById(renwuJincheng);



    }

    @Override
    public void deletet(List<Long> ids) {

        //1.删除任务
        LambdaQueryWrapper<Renwu> a=new LambdaQueryWrapper<>();
        a.in(Renwu::getId,ids);
        a.eq(Renwu::getStatus,1);
        int count = this.count(a);
        if (count>0){
            throw new CustomException("任务正在开启中，无法删除");
        }
        renWuService.removeByIds(ids);
        //2.删除任务关联的维度
        LambdaQueryWrapper<RenwuWeidu> weidu=new LambdaQueryWrapper<>();
        weidu.in(RenwuWeidu::getRenwuId,ids);
        renwuWeiduService.remove(weidu);
        //3.删除任务关联的任务进程
        LambdaQueryWrapper<RenwuJincheng> jc=new LambdaQueryWrapper<>();
        jc.in(RenwuJincheng::getRenwuId,ids);
        renwuJinchengService.remove(jc);

    }
}
